package valve_test.transportation;


import valve_test.DAO.Factory;
import valve_test.logic.*;
import valve_test.util.HibernateUtil;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.SQLException;
import java.util.List;

/**
 * Класс импорта, экспорта содержания БД
 * в файл формата xml
 */
public class OpenSaveXML {

    /**
     * Сохранение в определенное место
     * @param fileName - путь к файлу
     * @return  true - если сохранение выполнено успешно
     */
    public boolean saveInFile(String fileName) throws SQLException {

        Container container = new Container();

        container.setValvesList(Factory.getInstance().getValvesDAO().getAllValves());
        container.setIerarchyList(Factory.getInstance().getIerarchyDAO().getAllIerarchies());
        // Сохранение
        try{
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            XMLEncoder encoder = new XMLEncoder(fileOutputStream);
            encoder.writeObject(container);
            encoder.flush();
            encoder.close();

        } catch (FileNotFoundException exp)
        {
            return false;
        }
        return true;
    }

    public boolean openFromFile(String fileName) throws SQLException {

        Container dataFromFile;

        try {
            FileInputStream fileInputStream = new FileInputStream(fileName);
            XMLDecoder decoder = new XMLDecoder(fileInputStream);
            dataFromFile = (Container)decoder.readObject();
            decoder.close();
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
            return false;
        }
        deleteAll();
        valves val = new valves();
       // val.setV_id(3l);
      //  val.setV_name("qwe");
      //  val.setInput(3l);
      //  val.setOutput(4l);
      //  Factory.getInstance().getValvesDAO().addValve(val);
        boolean flag = true;
        for (valves valv: dataFromFile.getValvesList()) {
         //   if(flag){
          //      Factory.getInstance().getValvesDAO().addValveNew(valv);
          //      flag = false;
         //   }
         //   else
                Factory.getInstance().getValvesDAO().addValve(valv);
        }

        List<valves> l = Factory.getInstance().getValvesDAO().getAllValves();
        long number = l.get(0).getv_id() -1;
        long number2 = l.get(l.size()-1).getv_id();


        for (ierarchy ierar : dataFromFile.getIerarchyList()) {
            ierar.setC_id(ierar.getC_id()+number);
            ierar.setP_id(ierar.getP_id()+number);
            Factory.getInstance().getIerarchyDAO().addIerarchy(ierar);
        }      //  */
        return true;
    }
    public void deleteAll() throws SQLException {

        Container container = new Container();
        container.setValvesList(Factory.getInstance().getValvesDAO().getAllValves());
        container.setIerarchyList(Factory.getInstance().getIerarchyDAO().getAllIerarchies());

        for (ierarchy ierar : container.getIerarchyList()) {
            Factory.getInstance().getIerarchyDAO().deleteIerarchy(ierar);
        }
        for (valves valv : container.getValvesList()) {
            Factory.getInstance().getValvesDAO().deleteValve(valv);
        }
        HibernateUtil.restart();

    }


}
